package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.JGOrderDetailInfo;
import com.yy.jhl.ui.manager.contract.JGOrderDetailContract;
import com.yy.jhl.ui.manager.presenter.JGOrderDetailPresenter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/18.
 */

public class JGOrderDetailActivity extends BaseActivity<JGOrderDetailPresenter> implements JGOrderDetailContract.View {
    @BindView(R.id.order_name_text)
    TextView orderNameText;
    @BindView(R.id.a_text)
    TextView aText;
    @BindView(R.id.b_text)
    TextView bText;
    @BindView(R.id.product_parent)
    LinearLayout productParent;
    @BindView(R.id.c_text)
    TextView cText;
    @BindView(R.id.d_text)
    TextView dText;
    @BindView(R.id.e_text)
    TextView eText;
    @BindView(R.id.f_text)
    TextView fText;
    @BindView(R.id.g_text)
    TextView gText;
    @BindView(R.id.h_text)
    TextView hText;
    @BindView(R.id.i_text)
    TextView iText;
    @BindView(R.id.j_text)
    TextView jText;
    @BindView(R.id.k_text)
    TextView kText;
    @BindView(R.id.l_text)
    TextView lText;
    @BindView(R.id.m_text)
    TextView mText;
    @BindView(R.id.o_text)
    TextView oText;
    @BindView(R.id.p_text)
    TextView pText;
    @BindView(R.id.q_text)
    TextView qText;
    @BindView(R.id.r_text)
    TextView rText;
    @BindView(R.id.product_text)
    TextView productText;
    @BindView(R.id.order_product_more_text)
    TextView productMoreText;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    @BindView(R.id.home_parent)
    LinearLayout homeParent;
    @BindView(R.id.foreign_parent)
    LinearLayout foreignParent;
    @BindView(R.id.address_parent)
    LinearLayout addressParent;
    @BindView(R.id.gys_content_parent)
    LinearLayout gysContentParent;
    private String id = "";
    JGOrderDetailInfo jgInfo;
    private int currentLoginType = 1;
    boolean isDone;
    public static void startAction(Context mContext, String id,boolean isDone) {
        Intent intent = new Intent(mContext, JGOrderDetailActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("isDone", isDone);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_jg_order_detail;
    }

    @Override
    public void initView() {
        if (getIntent().getBooleanExtra("isDone",false)){
            ensureBtn.setVisibility(View.GONE);
        }
        currentLoginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        id = getIntent().getStringExtra("id");
        ensureBtn.setText(currentLoginType == 2 ? "进入监管流程" : "进入任务流程");
        showTitle(currentLoginType == 2 ? "监管订单" : "任务订单", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        mPresenter.getProjectDetail(id);
    }

    @Override
    public void getProjectDetail(final JGOrderDetailInfo info) {
        jgInfo = info;
        if (currentLoginType == 1){
            gysContentParent.setVisibility(View.GONE);
        }else {
//            gysContentParent.setVisibility(View.VISIBLE);
            gysContentParent.setVisibility(View.GONE);
        }
        orderNameText.setText(info.getProject_name());
        bText.setText(info.getOrder_sn());
        String orderInfo = info.getOrder_info().getItems();
        int orderTotal = info.getOrder_info().getTotal();
        if (orderTotal > 2){
            productMoreText.setVisibility(View.VISIBLE);
        }else {
            productMoreText.setVisibility(View.GONE);
        }
        StringBuffer orderInfoBuffer = new StringBuffer();
        if (!StringUtil.isEmpty(orderInfo)){
            String[] infos = orderInfo.split("--");
            for (int i=0;i<infos.length;i++){
                orderInfoBuffer.append(infos[i]);
                orderInfoBuffer.append("\n");
            }
        }
        productText.setText(orderInfoBuffer.toString());
        cText.setText(info.getDeadline());
        dText.setText(info.getFlow_content());
//        JGOrderDetailInfo.LogisticsContentBean logisticsInfo = info.getLogistics_content();
//        eText.setText(logisticsInfo.getFlow());
        String logisticsInfo = info.getLogistics_content();
        eText.setText(logisticsInfo);
        //指定供应商
        lText.setText(info.getCompany_supply_name());
        //备注
        mText.setText(info.getRemark());
        //收货人
        List<JGOrderDetailInfo.ShipAddressBean> shipAddress = info.getShip_address();
        if (shipAddress != null && shipAddress.size() > 0){
            addressParent.setVisibility(View.VISIBLE);
            JGOrderDetailInfo.ShipAddressBean addressBean = shipAddress.get(0);
            oText.setText(addressBean.getCompany_name());
            pText.setText(addressBean.getContact());
            qText.setText(addressBean.getAddress_info());
            rText.setText(addressBean.getPhone());
        }else {
            addressParent.setVisibility(View.GONE);
        }
        //国内，国外货代
//        String address = logisticsInfo.getAddress();
//        try {
//            JSONArray addressArray = new JSONArray(address);
//            if (addressArray != null && addressArray.length() > 0){
//                int length = addressArray.length();
//                if (length == 1){
//                    JSONObject result = addressArray.optJSONObject(0);
//                    if (result.optString("type").equals("1")){
//                        //国内
//                        homeParent.setVisibility(View.VISIBLE);
//                        foreignParent.setVisibility(View.GONE);
//                        fText.setText(result.optString("company_name"));
//                        gText.setText(result.optString("contact"));
//                        hText.setText(result.optString("phone"));
//                    }else {
//                        homeParent.setVisibility(View.GONE);
//                        foreignParent.setVisibility(View.VISIBLE);
//                        iText.setText(result.optString("company_name"));
//                        jText.setText(result.optString("contact"));
//                        kText.setText(result.optString("phone"));
//                    }
//                }else if (length == 2){
//                    for (int i=0;i<length;i++){
//                        JSONObject result01 = addressArray.optJSONObject(i);
//                        if (result01.optString("type").equals("1")){
//                            //国内
//                            homeParent.setVisibility(View.VISIBLE);
//                            fText.setText(result01.optString("company_name"));
//                            gText.setText(result01.optString("contact"));
//                            hText.setText(result01.optString("phone"));
//                        }
//                        if (result01.optString("type").equals("2")){
//                            foreignParent.setVisibility(View.VISIBLE);
//                            iText.setText(result01.optString("company_name"));
//                            jText.setText(result01.optString("contact"));
//                            kText.setText(result01.optString("phone"));
//                        }
//                    }
//                }
//            }else {
//                homeParent.setVisibility(View.GONE);
//                foreignParent.setVisibility(View.GONE);
//            }
//
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        productMoreText.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ProductListViewWebViewActivity.startAction(mContext,info.getOrder_id(),"");
            }
        });
    }

    @Override
    public void getProjectDetailFail() {

    }

    @OnClick(R.id.ensure_btn)
    public void onViewClicked() {
        OrderFlowActivity.startAction(mContext,id);
    }
}
